package org.light.framework.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件读取类
 * @author de.cel
 *
 */
public class PropsUtil {

	public static Properties load(String fileName) {
		Properties props = null;
		InputStream is = null;
		try {
			//System.out.println(fileName);
			
			is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
			if (is == null) {
				throw new FileNotFoundException(fileName + "，文件不存在");
			}
			props = new Properties();
			props.load(is);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO: handle exception
				}
			}
		}
		return props;
	}

	public static String getString(Properties props, String key) {
		return getString(props, key, "");
	}

	public static String getString(Properties props, String key, String defaultValue) {
		String value = defaultValue;
		if (props != null &&props.containsKey(key)) {
			value = props.getProperty(key);
		}
		return value;
	}

	public static int getInt(Properties props, String key) {
		return getInt(props, key, 0);
	}

	public static int getInt(Properties props, String key, int defaultValue) {
		int value = defaultValue;
		if (props != null &&props.containsKey(key)) {
			value = StringUtil.toInt(props.getProperty(key));
		}
		return value;
	}

	public static boolean getBoolean(Properties props, String key) {
		return getBoolean(props, key, false);
	}

	public static boolean getBoolean(Properties props, String key, boolean defaultValue) {
		boolean value = defaultValue;
		if (props != null && props.containsKey(key)) {
			value = StringUtil.toBoolean(props.getProperty(key));
		}
		return value;
	}
}
